package com.rongxun.hbwallet.mvp.presenter;

import android.support.annotation.NonNull;

import com.rongxun.base.BasePresenter;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.mvp.contract.ITransfer;
import com.rongxun.utils.RxUtil;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.TransactionUtils;
import com.rongxun.utils.wallet.WalletSubscriber;

import java.math.BigDecimal;
import java.math.BigInteger;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/20 13:54
 * 描述	      转账充值
 */

public class TransferPresenter extends BasePresenter<ITransfer.IView, ITransfer.IModel> implements ITransfer.IPresenter {
    public TransferPresenter(@NonNull ITransfer.IView pUi) {
        super(pUi);
    }

    public TransferPresenter(@NonNull ITransfer.IView pUi, @NonNull ITransfer.IModel pModel) {
        super(pUi, pModel);
    }

    @Override
    public void getEthBalance(String fromAddress) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<BigInteger>) e -> {
            BigInteger ethBalance = TransactionUtils.getEthBalance(fromAddress);
            e.onNext(ethBalance);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<BigInteger>(getView()) {
                    @Override
                    public void onSuccess(BigInteger bigInteger) {
                        getView().showEthBalance(bigInteger);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));

    }

    @Override
    public void getCityBalance(String fromAddress) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<BigInteger>) e -> {
            BigInteger ethBalance = TransactionUtils.getCityBalance(fromAddress);
            e.onNext(ethBalance);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<BigInteger>(getView()) {
                    @Override
                    public void onSuccess(BigInteger bigInteger) {
                        getView().showCityBalance(bigInteger);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public void getGasPrice() {

        addSubscribe(Flowable.create((FlowableOnSubscribe<BigInteger>) e -> {
            BigInteger ethBalance = TransactionUtils.getTransactionGasPrice();
            e.onNext(ethBalance);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<BigInteger>(getView()) {
                    @Override
                    public void onSuccess(BigInteger bigInteger) {
                        getView().showGasPrice(bigInteger);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));

    }

    @Override
    public void getGasLimit(String fromAddress) {

    }

    @Override
    public void transfer(String password, String fromAddress, String toAddress, BigInteger amount, BigInteger gasLimit, BigInteger gasPrice) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<String>) e -> {
            String txHash = TransactionUtils.sendTokenTransaction(fromAddress, password, toAddress, amount, gasLimit, gasPrice);
            e.onNext(txHash);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<String>(getView()) {
                    @Override
                    public void onSuccess(String txHash) {
                        getView().showTransferResult(txHash);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public void recharge(String password, String fromAddress, String toAddress, BigDecimal amount, BigInteger gasLimit, BigInteger gasPrice) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<String>) e -> {
            String txHash = TransactionUtils.sendTransaction(fromAddress, password, toAddress,gasPrice,gasLimit,amount);
            e.onNext(txHash);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<String>(getView()) {
                    @Override
                    public void onSuccess(String txHash) {
                        getView().showRechargeResult(txHash);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public void checkTransferInfo(String address, String transfer, BigInteger balance) {
        if (address.isEmpty()) {
            ToastUtils.showToast(R.string.no_address);
            return;
        }


        if (transfer.isEmpty()) {
            ToastUtils.showToast(R.string.no_money);
            return;
        }

//        if (transfer.equals("0")){
//            ToastUtils.showToast(R.string.no_money);
//            return;
//        }

        BigDecimal tr = new BigDecimal(transfer);
        BigDecimal ba = new BigDecimal(balance);


        if (balance.equals("0") || ba.compareTo(tr) == -1) {
            ToastUtils.showToast(R.string.not_sufficient_funds);
            return;
        }

        getView().showTransferInfo();
    }
}
